package com.cake.liulanxiangzhu.service.impl;

import com.alibaba.fastjson.JSON;
import com.cake.liulanxiangzhu.exception.ServiceException;
import com.cake.liulanxiangzhu.mapper.AdminCategoryMapper;
import com.cake.liulanxiangzhu.mapper.CakeProductMapper;
import com.cake.liulanxiangzhu.pojo.dto.CakeProductDTO;
import com.cake.liulanxiangzhu.pojo.dto.CakeProductInsertDTO;
import com.cake.liulanxiangzhu.pojo.entity.CakeProduct;
import com.cake.liulanxiangzhu.pojo.vo.AdminCategoryVO;
import com.cake.liulanxiangzhu.pojo.vo.CakeProductVO;
import com.cake.liulanxiangzhu.service.ICakeProductService;
import com.cake.liulanxiangzhu.utils.DataUseUtil;
import com.cake.liulanxiangzhu.web.JsonPage;
import com.cake.liulanxiangzhu.web.JsonResult;
import com.cake.liulanxiangzhu.web.ProductCode;
import com.cake.liulanxiangzhu.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class CakeProductServiceImpl implements ICakeProductService {

    @Autowired
    CakeProductMapper cakeProductMapper;

    @Autowired
    AdminCategoryMapper categoryMapper;

    @Autowired
    RedisTemplate redisTemplate;




    @Override
    public void addCake(CakeProductInsertDTO cakeProductInsertDTO  ) {
        List<CakeProductVO> list = cakeProductMapper.list();
        String cakeNameNew = cakeProductInsertDTO.getCakeName();
        for (CakeProductVO cakeProductVO : list) {
            String cakeNameOld = cakeProductVO.getCakeName();
            if (cakeNameOld.equals(cakeNameNew)){
                throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"产品名称相同，请重新输入");
            }
        }
        CakeProduct cakeProduct = new CakeProduct();
        BeanUtils.copyProperties(cakeProductInsertDTO , cakeProduct);
        cakeProduct.setCreatTime(LocalDateTime.now());
        cakeProduct.setUpdateTime(LocalDateTime.now());
        cakeProductMapper.insertCakeProduct(cakeProduct);
    }

    @Override
    public JsonPage<CakeProductVO> allCakes(Integer page , Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<CakeProductVO> list = cakeProductMapper.list();
        for (CakeProductVO cakeProductVO : list) {
            Integer categoryId = cakeProductVO.getCategoryId();
            AdminCategoryVO category = categoryMapper.getCategoryByID(categoryId);
            String categoryName = category.getCategoryName();
            cakeProductVO.setCategoryName(categoryName);
            Integer isPublished = cakeProductVO.getIsPublished();
            cakeProductVO.setIsPublishedNm(ProductCode.getDesc(isPublished));
            Integer isNew = cakeProductVO.getIsNew();
            cakeProductVO.setIsNewNm(ProductCode.getDesc(isNew));

        }
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public List<CakeProductVO> listSpu() {

        List<CakeProductVO> list = cakeProductMapper.list();
        return list;
    }

    @Override
    public void deleteCake(Integer cakeId) {
        CakeProductVO vo = cakeProductMapper.getCakeById(cakeId);
        if (vo == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"无此ID信息");
        }
        String cakeIcon = vo.getCakeIcon();
        new File("e:/files" + cakeIcon).delete();
        int i = cakeProductMapper.deleteCakeById(cakeId);
        if (i!=1){
            throw new ServiceException(ServiceCode.ERR_DELETE,"系统繁忙，请稍后再试");
        }
    }

    @Override
    public void updateCake(CakeProductDTO cakeProductDTO ,Integer cakeId) {
        CakeProductVO vo = cakeProductMapper.getCakeById(cakeId);
        if (vo !=null){
            String cakeIcon = cakeProductDTO.getCakeIcon();
            String voCakeIcon = vo.getCakeIcon();
            if (DataUseUtil.isNotBlank(cakeIcon)){
                if (!cakeIcon.equals(voCakeIcon)){
                    new File("e:/files" + voCakeIcon).delete();
                }
            }
            CakeProduct cakeProduct = new CakeProduct();
            BeanUtils.copyProperties(cakeProductDTO,cakeProduct);
            cakeProduct.setCakeId(cakeId);
            cakeProduct.setUpdateTime(LocalDateTime.now());
            int i = cakeProductMapper.updateCakeById(cakeProduct);
            if (i!=1){
                throw new ServiceException(ServiceCode.ERR_DELETE,"系统繁忙，请稍后再试");
            }
        }else {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"无此ID信息");
        }
    }

    @Override
    public void updateDownPublished(Integer cakeId) {
        CakeProductVO cakeById = cakeProductMapper.getCakeById(cakeId);
        if (cakeById!=null){
            Integer isPublished = cakeById.getIsPublished();
            System.out.println(isPublished);
            if (ProductCode.YES.equals(isPublished)){
                CakeProduct cakeProduct = new CakeProduct();
                cakeProduct.setCakeId(cakeId);
                cakeProduct.setIsPublished(ProductCode.NO.getCode());
                cakeProduct.setUpdateTime(LocalDateTime.now());
                cakeProductMapper.updateCakeById(cakeProduct);
            }else {
                throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"已经是下架状态了");
            }
        }else {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"无此ID信息");
        }

    }

    @Override
    public void updateSendPublished(Integer cakeId) {
        CakeProductVO cakeById = cakeProductMapper.getCakeById(cakeId);
        if (cakeById!=null){
            Integer isPublished = cakeById.getIsPublished();
            System.out.println(isPublished);
            if (ProductCode.NO.equals(isPublished)){
                CakeProduct cakeProduct = new CakeProduct();
                cakeProduct.setCakeId(cakeId);
                cakeProduct.setIsPublished(ProductCode.YES.getCode());
                cakeProduct.setUpdateTime(LocalDateTime.now());
                cakeProductMapper.updateCakeById(cakeProduct);
            }else {
                throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"已经是上架状态了");
            }
        }else {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"无此ID信息");
        }
    }

    @Override
    public CakeProductVO queryCake(Integer cakeId) {

//        Boolean ceshi = redisTemplate.opsForValue().setIfAbsent("ceshiaa", UUID.randomUUID().toString(), 2, TimeUnit.MINUTES);
//        if (!ceshi){
//            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"请稍后重试");
//        }

        String pms = (String) redisTemplate.opsForValue().get(CAKE_PMS + cakeId);
        if (DataUseUtil.isNotBlank(pms)){
            CakeProductVO cakeProductVO = JSON.parseObject(pms, CakeProductVO.class);
            redisTemplate.expire(CAKE_PMS+cakeId,60, TimeUnit.MINUTES);
            return cakeProductVO;
        }else {
            CakeProductVO vo = cakeProductMapper.getCakeById(cakeId);
            if (vo!=null){
                redisTemplate.opsForValue().set(CAKE_PMS+cakeId, JSON.toJSONString(vo),60, TimeUnit.MINUTES);
                return vo;
            }else {
             throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"没有查询到指定的商品，请选择正确商品");
            }
        }
    }

    @Override
    public List<CakeProductVO> querySale() {
        List<CakeProductVO> vos = cakeProductMapper.queryBySales();
        if (vos.size()!=3){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"销量数据异常");
        }
        return vos;
    }

    @Override
    public List<CakeProductVO> queryPms(Integer categoryId) {
        List<CakeProductVO> productVOS = cakeProductMapper.listWx(categoryId);
        return productVOS;
    }

    @Override
    public List<CakeProductVO> queryProductByCategoryId(Integer categoryId) {
        return null;
    }


}
